package com.olap.starter.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.olap.starter.config.dataSource.JdbcTemplateSource;
import com.olap.starter.core.base.BaseService;
import com.olap.starter.core.response.Result;
import com.olap.starter.core.response.ResultUtil;
import com.olap.starter.mapper.OnlineDictTypeMapper;
import com.olap.starter.pojo.OnlineDictData;
import com.olap.starter.pojo.OnlineDictType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author WBG
 * @since 2022-11-28
 */
@Service
@DS("online")
public class OnlineDictTypeServices extends BaseService<OnlineDictTypeMapper, OnlineDictType> {

    @Autowired
    private JdbcTemplateSource jdbcTemplateSource;

    public Result listOnline() {
        List<OnlineDictType> list = getBaseMapper().listOnline();
        return ResultUtil.success(list);
    }

    public Result listDict() {
        List<OnlineDictType> list = list(new LambdaQueryWrapper<OnlineDictType>().eq(OnlineDictType::getStatus,0));
        toAddCloud(list);
        return ResultUtil.success(list);
    }

    private void toAddCloud(List<OnlineDictType> list) {
        //嵌套微服务
        JdbcTemplate jdbcTemplate = jdbcTemplateSource.getJdbcTemplateByDbName("olp_cloud");
        if (jdbcTemplate != null) {
            List<Map<String, Object>> maps = jdbcTemplate.queryForList("select NAME,TYPE from system_dict_type where status = 0");
            for (Map<String, Object> obj : maps) {
                OnlineDictType onlineDictType = new OnlineDictType();
                if (obj.get("NAME") != null) {
                    onlineDictType.setName(obj.get("NAME").toString());
                }
                if (obj.get("TYPE") != null) {
                    onlineDictType.setCode(obj.get("TYPE").toString());
                }
                list.add(onlineDictType);
            }
        }
    }
}
